<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <common-head></common-head>
  </div>
  <script src="./vue.js"></script>
  <script>
    // 定义一个第三方的实例
    const bus = new Vue();
    const Child1 = {
      template:`
        <h3>
          我是子组件 1
          <button @click="emit">向兄弟组件发送数据</button>
        </h3>
      `,
      data(){
        return {
          msg: '子组件1中的数据'
        }
      },
      methods: {
        emit(){
          bus.$emit('piupiu', this.msg)
        }
      }
    }
    const Child2 = {
      template:`
        <h3>
          我是子组件 2
          {{ msg }}
        </h3>
      `,
      data(){
        return {
          msg: ''
        }
      },
      mounted(){

        bus.$on('piupiu', (msg)=>{
          this.msg = msg;
        }) 
        /* const _this = this;
        bus.$on('piupiu', function(msg){
          this.msg = msg;
        }.bind(this)) */
      }
    }
    const CommonHead = {
      /* 
      定义组件的 结构 
      */
      template:`
        <div>
          <h1>我是父组件</h1>
          <child1></child1>
          <hr/>
          <child2></child2>
        </div>
      `,
      components: {
        Child1,
        Child2
      }
    }
    Vue.component('CommonHead', CommonHead)
    const vm = new Vue({
      el: '#app'
    })

    /* function fn(){
      console.log(this);
    }
    fn.call(2)
    var fn2 = fn.bind(3)
    fn2(); */

  </script>
</body>
</html>